import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { Observable } from 'rxjs';
import { UserModel } from '../../@common/models/user.model';
import { SignInModel } from '../../@common/models/sign-in.model';
import { SignUpModel } from '../../@common/models/sign-up.model';

@Injectable()
export class AuthService {
  private readonly apiUrl: string;
  constructor(private readonly httpClient: HttpClient) {
    this.apiUrl = environment.apiUrl + 'auth';
  }

  fetchUserToken = (): string | null => sessionStorage.getItem('user-token');

  setUserToken = (token: string): void => {
    sessionStorage.setItem('user-token', token);
  };

  signIn = (signIn: SignInModel): Observable<UserModel> =>
    this.httpClient.post<UserModel>(this.apiUrl, signIn);

  signUo = (signUp: SignUpModel): Observable<UserModel> => {
    const url = `${this.apiUrl}/sign-up`;
    return this.httpClient.post<UserModel>(url, signUp);
  };
}
